<?php
/**
 * Created by PhpStorm.
 * User: xiaxiang
 * Date: 2019/3/14
 * Time: 13:58
 */

namespace App\Http\Controllers\Admin;


use App\Category;
use Illuminate\Http\Request;


class CategoryController extends BaseController
{

    public function add()
    {
        $arr = Category::getCategoryAll(0);
        return view('admin.category.add', ['arr' => $arr]);
    }

    //添加
    public function doadd(Request $request)
    {
        //接收表单传值
        $arr = $request->all();
        unset($arr['_token']);
        $re = Category::insert($arr);
        $message = $re ? "添加成功" : "添加失败";
        return redirect()->back()->with('message', $message);
    }

    //列表
    public function index()
    {
        $arr = Category::getCategoryAll(0);
        return view('admin.category.index', ['arr' => $arr]);
    }

    //删除
    public function del($id)
    {
        $num = Category::where('pid', $id)->count();
        if ($num == 0) {
            $re = Category::where('id', $id)->delete();
            $message = $re ? "删除成功" : "删除失败";

        } else {
            $message = "请 先删除子子分类";
        }
        return redirect()->back()->with('message', $message);
    }

    //编辑
    public function edit($id){
        $arr = Category::getCategoryAll(0);
        $ob = Category::where('id',$id)->first();
        return view('admin.category.edit',['ob'=>$ob,'arr'=>$arr]);
    }

    public function doedit(Request $request){
            $arr = $request->all();
            $id = $arr['id'];
            unset($arr['_token']);
            unset($arr['id']);
            $re  = Category::where('id',$id)->update($arr);
            $message = $re ? '修改成功' : "修改失败";
            return redirect(url('admin/category/index'))->with('message',$message);
    }
}


